package com.wy.component.office;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;


public class ExportExcelUtils<T>{
	/**
	 * 这是一个通用的方法,将一个map集合作为表格内容输入到excel中
	 * 
	 * @param title
	 *            表格标题名
	 * @param headers
	 *            表格属性列名数组
	 * @param rows
	 *            需要显示的数据集合,row为List<Map>
	 * @param rowkeys
	 * 			      与headers对应的map的key的集合数组      
	 * @param conditionsDescrip
	 * 			   xls说明
	 * @param out
	 *            与输出设备关联的流对象，可以将EXCEL文档导出到本地文件或者网络中
	 */
	public void exportExcel(String title, String[] headers,List<Map> rows, String[] rowkeys, OutputStream out) {
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet(title);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth((short) 25);
		
		/** ----------- 生成标题     ------------*/
		//第一行写入标题
//		HSSFFont fontTitle = workbook.createFont();
//		HSSFCellStyle titleStyle = workbook.createCellStyle();
//		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//		titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//		fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//		fontTitle.setFontHeightInPoints((short) 16);
//		titleStyle.setFont(fontTitle);
		
//		sheet.addMergedRegion(new Region(0,(short)0,0,(short)(headers.length-1)));
//		HSSFRow rowTitle = sheet.createRow(0);
//		rowTitle.setHeight((short)1000);
//		HSSFCell cellTitle = rowTitle.createCell(0);
//		cellTitle.setCellStyle(titleStyle);
//		cellTitle.setCellValue(title);
		
		/** ----------- 生成表头     ------------*/
		HSSFFont fontHeader = workbook.createFont();
		fontHeader.setBoldweight(Font.BOLDWEIGHT_BOLD);
		HSSFCellStyle cstyleHeader =workbook.createCellStyle();
		cstyleHeader.setFont(fontHeader);
		cstyleHeader.setAlignment(CellStyle.ALIGN_CENTER);
		cstyleHeader.setBorderBottom(CellStyle.BORDER_THIN);
		cstyleHeader.setBorderLeft(CellStyle.BORDER_THIN);
		cstyleHeader.setBorderRight(CellStyle.BORDER_THIN);
		cstyleHeader.setBorderTop(CellStyle.BORDER_THIN);
		HSSFRow row = sheet.createRow(0);
		for (short i = 0; i < headers.length; i++) {
			HSSFCell cell = row.createCell(i);
			//cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellStyle(cstyleHeader);
			cell.setCellValue(text);
		}
		
		/** ----------- 生成数据行     ------------*/
		int index = 1;
		HSSFFont fontData = workbook.createFont();
		fontData.setBoldweight(Font.BOLDWEIGHT_NORMAL);
		HSSFCellStyle cstyleData =workbook.createCellStyle();
		cstyleData.setBorderBottom(CellStyle.BORDER_THIN);
		cstyleData.setBorderLeft(CellStyle.BORDER_THIN);
		cstyleData.setBorderRight(CellStyle.BORDER_THIN);
		cstyleData.setBorderTop(CellStyle.BORDER_THIN);
		cstyleData.setFont(fontData);
		cstyleData.setAlignment(CellStyle.ALIGN_CENTER);
		
/*		HSSFCellStyle cstyleData1 =workbook.createCellStyle();
		cstyleData1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cstyleData1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cstyleData1.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cstyleData1.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cstyleData1.setFont(fontData);
		cstyleData1.setAlignment(HSSFCellStyle.ALIGN_LEFT);*/
		for(Map row1 : rows){
			row = sheet.createRow(index);
			//获取map的所有需要导入到excel中数据key值
			for(int i = 0 ; i < rowkeys.length ; i++){
				HSSFCell cell = row.createCell(i);
//				if(i==0||i==3||i==4||i==5||i==6||i==7||i==8){
				cell.setCellStyle(cstyleData);
//				}else{
//					cell.setCellStyle(cstyleData1);
//				}
				String key = rowkeys[i];
				String cellValue = (String) row1.get(key);
				cell.setCellValue(cellValue);
			}
			index++;
		}
		
		/** ----------- 生成说明行     ------------*/
		HSSFFont fontDesc = workbook.createFont();
		HSSFCellStyle descStyle =workbook.createCellStyle();
		descStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		descStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		descStyle.setAlignment(CellStyle.ALIGN_LEFT);
		descStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
		fontDesc.setFontHeightInPoints((short) 9);
		descStyle.setFont(fontDesc);
	
		//最后行写入描述
		/*
		sheet.addMergedRegion(new Region(index,(short)0,index,(short)(headers.length-1)));
		HSSFRow rowDesc = sheet.createRow(index);
		rowDesc.setHeight((short)1500);
		HSSFCell cellDesc = rowDesc.createCell(0);
		cellDesc.setCellStyle(descStyle);
		descStyle.setWrapText(true);
		cellDesc.setCellValue(new HSSFRichTextString(conditionsDescrip));
        */
		try {
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
